package com.alex.serializer;

import java.util.Date;
import java.util.Properties;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.serialization.StringDeserializer;
import org.apache.kafka.common.serialization.StringSerializer;

/**
 * @ClassName KafkaProducerInterceptor
 * @Description TODO
 * @Author zjg
 * @Date 2021/12/615:28
 * @Version 2.0
 */

public class KafkaProducerQuickStart {
    public static void main(String[] args) {
    // 创建KakfaProducer
      Properties props = new Properties();
      props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG,"49.233.172.9:9092");
      props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
      props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,UserDefineSerializer.class.getName());

      KafkaProducer<String, User> producer = new KafkaProducer<String, User>(props);


      for(int i =0;i<10;i++){
        ProducerRecord<String, User> record ;
        record = new ProducerRecord<String, User>("topic02",
            "key" + i, new User(i,"user"+i,new Date()));
        producer.send(record);
      }
      producer.close();
    }
}
